package com.vitabuybuy.tv.ui.cart.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemShopMallBinding
import com.vitabuybuy.tv.dto.CartGoodsDetailsDto
import com.library.common.base.BindRecycAdapter

class ShopMallAdapter : BindRecycAdapter<ItemShopMallBinding, CartGoodsDetailsDto>() {

    lateinit var cartGoodsAdapter: CartGoodsAdapter
    lateinit var shopHintAdapter: ShopHintAdapter

    override fun onBindViewHolder(
        mBinding: ItemShopMallBinding,
        itemData: CartGoodsDetailsDto,
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        mBinding.itemData = itemData
        //商品列表
        cartGoodsAdapter = CartGoodsAdapter()
        mBinding.lvGoods.apply {
            adapter = cartGoodsAdapter
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
        cartGoodsAdapter.setNewData(itemData.itemList)

        //促销数据列表
        shopHintAdapter = ShopHintAdapter()
        mBinding.lvShopHint.apply {
            adapter = shopHintAdapter
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
        shopHintAdapter.setNewData(arrayListOf(""))

        cartGoodsAdapter.onAddClick = { cartId, itemPosition ->
            onAddClick(cartId, itemPosition, position)
        }
        cartGoodsAdapter.onReductClick = { cartId, itemPosition ->
            onReductClick(cartId, itemPosition, position)
        }
        mBinding.ivChoose.setOnClickListener {
            //现更改子item的选中状态
            for (item in itemData.itemList) {
                item.isChecked = !itemData.isChecked
            }
            cartGoodsAdapter.notifyDataSetChanged()
            //更改自己控件的状态
            itemData.isChecked = !itemData.isChecked
            notifyItemChanged(position)
            //主布局监听
            onClickParentChoose()
        }
        cartGoodsAdapter.onItemClickChoose = { childPosition ->
            //更改子item控件的状态
            itemData.itemList[childPosition].isChecked = !itemData.itemList[childPosition].isChecked
            cartGoodsAdapter.notifyItemChanged(position)
            //计算是否全部选中
            var clickNum = 0
            for (item in itemData.itemList) {
                if (item.isChecked) {
                    clickNum++
                }
            }
            //根据知否自己全部选中而选中父布局
            itemData.isChecked = clickNum == itemData.itemList.size
            notifyItemChanged(position)
            //监听通知
            onClickParentChoose()
        }
        mBinding.tvGetCoupons.setOnClickListener {
            onClickCoupons.invoke(position)
        }
    }

    lateinit var onReductClick: (cartId: Int, itemPosition: Int, parentPosition: Int) -> Unit
    lateinit var onAddClick: (cartId: Int, itemPosition: Int, parentPosition: Int) -> Unit
    lateinit var onClickCoupons: (position: Int) -> Unit
    lateinit var onClickParentChoose: () -> Unit

    override fun normalViewId(viewType: Int): Int {
        return R.layout.item_shop_mall
    }

}
